package com.collins.thread.practices.findPrime;

import java.util.ArrayList;
import java.util.concurrent.Callable;

public class FindPrimeThread implements Callable<ArrayList<Integer>> {
    public Integer bound;
    public ArrayList<Integer> numbers;
    public ArrayList<Integer> primes = new ArrayList<>();

    public FindPrimeThread(int interval, int bound, ArrayList<Integer> raw) {
        this.bound = bound;
        this.numbers = new ArrayList<Integer>(raw.subList(bound * interval, (bound + 1) * interval - 1));
    }

    @Override
    public ArrayList<Integer> call() throws Exception {
        for (Integer number : numbers) {
            if (!define(number)) {
                primes.add(number);
            }
        }
        return primes;
    }

    public static boolean define(int number) {
        for (int i = 2; i < number / 2; i++) {
            if (number % i == 0) {
                return true;
            }
        }
        return false;
    }
}
